package com.oceanread.cn.controller;

import com.alibaba.fastjson.JSONObject;
import com.oceanread.cn.config.MyConfig;
import com.oceanread.cn.config.Result;
import com.oceanread.cn.entity.Sediment;
import com.oceanread.cn.service.ISedimentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.tomcat.util.http.fileupload.util.Streams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

@RestController
@RequestMapping("/Sediment")
@CrossOrigin()
@Api(value = "", tags = {"2.5-底质材质"})
public class SedimentController {

    @Autowired
    private ISedimentService iSedimentService;
    /*获取总表*/
    @GetMapping("/getall")
    public List<Sediment> getList() {
        return iSedimentService.getall();
    }

    @GetMapping("/getList")
    public JSONObject getList(@RequestParam(name = "page", required = true, defaultValue = "1") int page,
                              @RequestParam(name = "size", required = true, defaultValue = "10") int size) {
        JSONObject object = new JSONObject();
        object.put("data", iSedimentService.getTablelist(page, size));
        object.put("total", iSedimentService.getnum());
        return object;
    }

    @GetMapping("/query1")
    public JSONObject query(
            @RequestParam(name = "page", required = true, defaultValue = "1") int page,
            @RequestParam(name = "size", required = true, defaultValue = "10") int size,
            @RequestParam(name = "key") String key,
            @RequestParam(name = "value") String value
    ) {
        JSONObject object = new JSONObject();
        object.put("data", iSedimentService.query(page,size,key,value));
        object.put("total", iSedimentService.getnumquery(key,value));
        return object;
    }
    @ApiOperation(value = "前端可视化获取info")
    @GetMapping("/getinfo")
    public List<Sediment> getinfo(
            @RequestParam(name = "PrjCode") String PrjCode
    ) {
        return iSedimentService.equal("PrjCode",PrjCode);
    }
    @PostMapping("/update")
    public Integer update(@RequestBody Sediment sediment) {
        return iSedimentService.update(sediment);
    }
    @PostMapping("/insert")
    public Integer insert(@RequestBody Sediment sediment) {
        return iSedimentService.insert(sediment.getPrjCode(),sediment.getCode(),sediment.getName(),sediment.getArea(),sediment.getTime(),
                sediment.getCenterCoordinateX(),sediment.getCenterCoordinateY(),sediment.getCenterCoordinateZ(),sediment.getUrl());
    }
    @GetMapping("/delete")
    public Integer delete(@RequestParam(name = "SYSID") int sysid) {
        return iSedimentService.delete(sysid);
    }

    @RequestMapping(value = "/uploadGeojson",method = RequestMethod.POST)
    @ResponseBody
    public Result uploadGeojson(@RequestParam("file") MultipartFile file) {
        String fileName = file.getOriginalFilename();
        String filetype = fileName.substring(fileName.lastIndexOf("."));
        if (filetype.equals(".json"))//处理xls文件
        {
            try {
                String rootPath = MyConfig.getFilepath() + "json/Sediment/";
                String storagePath = rootPath + file.getOriginalFilename();
                /*保存文件*/
                Streams.copy(file.getInputStream(), new FileOutputStream(storagePath), true);
                System.out.println(storagePath);
                return Result.success();
            } catch (IOException e) {
                return Result.error(e.getMessage());
            }
        }
        return Result.error();
    }

    @RequestMapping(value = "/getGeojsonfile/{JsonName}",produces = "text/plain;charset=UTF-8",method = RequestMethod.GET)
    @ResponseBody
    public byte[] getGeojsonfile( @PathVariable("JsonName") String JsonName) {
        try {
            System.out.println(JsonName);
            File file = new File(MyConfig.getFilepath() + "json/Sediment/" + JsonName);
            FileInputStream inputStream = new FileInputStream(file);
            byte[] bytes = new byte[inputStream.available()];
            inputStream.read(bytes, 0, inputStream.available());
            return bytes;
        } catch (Exception e) {
            return null;
        }
    }
    @ApiOperation(value = "Sediment统一插入")
    @RequestMapping(value = "/SedimentInsert", method = RequestMethod.POST)
    public void inserttest(HttpServletRequest request, HttpServletResponse response) {
        try {
            BufferedReader reader = request.getReader();
            if (reader != null) {
                String redaline = reader.readLine();
                JSONObject readobject = JSONObject.parseObject(redaline);
                JSONObject data = readobject.getJSONObject("data");
                System.out.println(data);
                String PrjCode = data.getString("PrjCode");
                String Name = data.getString("Name");

                String url = data.getString("url");
                Sediment sediment = new Sediment();
                sediment.setPrjCode(PrjCode);
                sediment.setName(Name);
                sediment.setUrl(url);
                iSedimentService.insert(sediment.getPrjCode(),sediment.getCode(),sediment.getName(),sediment.getArea(),sediment.getTime(),
                        sediment.getCenterCoordinateX(),sediment.getCenterCoordinateY(),sediment.getCenterCoordinateZ(),sediment.getUrl());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
